package com.geek.novel.activity.auth

import android.os.Bundle
import android.view.View
import com.geek.novel.R
import com.geek.novel.activity.auth.view.listener.PwdViewerClickListener
import com.geek.novel.activity.base.TitleBarActivity
import com.geek.novel.bo.AppContext
import com.geek.novel.common.Constant
import com.geek.novel.dialog.ConfirmDialog
import com.geek.novel.error.ValidateException
import com.geek.novel.listener.ApiResultListener
import com.geek.novel.services.manager.AuthManager
import com.geek.novel.utils.ToastUtils
import com.geek.novel.utils.ValidateUtil
import com.geek.novel.vo.ApiErrorResult
import com.geek.novel.vo.ApiResult
import kotlinx.android.synthetic.main.activity_forget_password.*

/**
 * 忘记密码页面
 */
class ForgetPasswordActivity: TitleBarActivity(), ApiResultListener {

	//用户信息管理器
	private lateinit var authManager: AuthManager

	/**确认框*/
	private lateinit var confirmDialog: ConfirmDialog

	override fun pageName(): String{
		return AppContext.getContext().getString(R.string.title_bar_forget_password)
	}
	
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_forget_password)

		//显示标题栏右侧按钮
		super.showTitleBtn("提交")

		//初始化确认弹框
		this.initConfirmDialog()

		//初始化用户信息管理器
		authManager = AuthManager()
		authManager.setOnApiResultListener(this)

		//显示隐藏密码
		iv_forget_password_visible.setOnClickListener(PwdViewerClickListener(et_forget_password))
		iv_forget_password_again_visible.setOnClickListener(PwdViewerClickListener(et_forget_password_again))
	}


	/**初始化确认弹框*/
	private fun initConfirmDialog(){
		confirmDialog = ConfirmDialog(this)
		confirmDialog.hideDismissBtn()
		//弹框一关闭就关闭当前页面
		confirmDialog.setOnDismissListener{
			this.finish()
		}
		confirmDialog.setOnConfirmListener("关闭") {
			confirmDialog.dismiss()
		}
	}

	/**标题右侧按钮的点击事件*/
	override fun onTitleBtnClick(view: View) {
		this.forgetPassword()
	}

	/**附加控件*/
	override fun attachViewChild() {
		btn_forget_send_validate_code.setOnCountDownClickListener{
			this.sendForgetPassValidateCode()
		}
	}

	/**
	 * 点击事件
	 */
	override fun clickEvent(id: Int, v: View) {
	}


	/**发送验证码*/
	private fun sendForgetPassValidateCode(){
		try {
			//校验信息
			val email = et_forget_email.text.toString()

			ValidateUtil.validateEmail(email, "电子邮箱")

			//发送忘记密码的验证码
			authManager.sendForgetPassValidateCode(email)

		} catch (e: Exception) {
			throw e
		}
	}

	/**调用接口保存用户信息*/
	private fun forgetPassword(){
		try {
			//校验信息
			val email = et_forget_email.text.toString()
			val code = et_forget_validate_code.text.toString()

			ValidateUtil.validateEmail(email, "电子邮箱")
			ValidateUtil.validateNotEmpty(code, "验证码", "请输入验证码！")

			val password = et_forget_password.text.toString()
			ValidateUtil.validateNotEmpty(password, "密码", "请输入密码！")
			ValidateUtil.validatePassword(password, "密码")

			val passwordAgain = et_forget_password_again.text.toString()
			ValidateUtil.validateNotEmpty(passwordAgain, "密码确认", "请再次输入密码！")
			ValidateUtil.validatePassword(passwordAgain, "密码确认")

			if(password != passwordAgain){
				throw ValidateException("两次输入密码不一致！")
			}

			//调用接口修改密码
			authManager.forgetPassword(email, code, password, passwordAgain)

		} catch (e: Exception) {
			ToastUtils.toast(e.message)
		}
	}

	/**销毁页面*/
	override fun onDestroy() {
		super.onDestroy()
		//销毁倒计时按钮的定时器
		btn_forget_send_validate_code.onDestroy()
	}

	/**处理错误信息*/
	override fun handleError(code: Int, err: ApiErrorResult) {
		when(code){
			Constant.HTTP_KEY_FORGET_PASSWORD_VALIDATE_CODE->{
				btn_forget_send_validate_code.cancelCountDown()
			}
		}
	}

	/**处理http请求响应结果*/
	override fun handleResult(result: ApiResult, code: Int) {
		when(code){
			Constant.HTTP_KEY_FORGET_PASSWORD_VALIDATE_CODE->{
				ToastUtils.toast("验证码发送成功！")
			}
			Constant.HTTP_KEY_FORGET_PASSWORD->{
				this.confirmDialog.openDialog("密码修改成功！")
			}
		}
	}

}